package servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求转发过程
 **/
public class RequestDispatchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求的参数（办事的材料）查看
        String username = request.getParameter("username");
        System.out.println("在RequestDispatchServlet(柜台1)中查看参数(材料): " + username);

        //给材料盖一个章，并传递到RequestDispatchServlet2(柜台2)中去查看
        request.setAttribute("key", "柜台1的章");

        //问路：RequestDispatchServlet2(柜台2)怎么走
        /*
        * 请求转发必须要以 / 斜杠打头，斜杠表示地址为：http://id:port/工程名/ ，映射到IDEA代码的web目录
        * */
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/dispatch2");

        //走向RequestDispatchServlet2(柜台2)
        requestDispatcher.forward(request, response);
    }
}
